import type { Request,Response  } from "express";// 引入express 内部的类型定义

import db from "../../db/index"

// 定义controller 函数类型
const indexController : {
    [key in string]: (req: Request, res: Response) => void;
} = {}

// 获取所有商品
indexController.getGoods = (req, res) => {
    db.query('SELECT * FROM goods', (err: any, response: Array<Object>) => {
        if(err) throw err
        // res.json(users)
        console.log("######################## 调用 /getGoods ########################")

        res.send({
            code: 1,
            msg: "查询成功",
            data: response
        })
    })
    
};

// 获取商品详情
indexController.getGoodsDetails = (req, res) => {
    let id = req.body.id || null
    if(id && id == 1){
        db.query(`SELECT * FROM goods WHERE id = ${id}`, (err: any, response: Array<Object>) => {
            if(err) throw err
            // res.json(users)
            console.log("######################## 调用 /getGoodsDetails ########################")
    
            res.send({
                code: 1,
                msg: "查询成功",
                data: response[0]
            })
        })

    } 
    if(id && id != 1){
        res.send({
            code: 1,
            msg: "未查询到指定商品",
            data: null
        });
    }
    if(!id){
        res.send({
            code: 0,
            msg: "入参不正确",
            data: null
        })
    }
}


export default indexController